//
//  CYTabBarVController.m
//  CreditAddressBook
//
//  Created by Lee on 15/4/22.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYTabBarVController.h"
#import "XCTabBarView.h"
#import "XCTabBarButton.h"
#import "UITabBar+Extension.h"
#import "CYNavigationVController.h"

@interface CYTabBarVController ()<XCTabBarDelegate>

@property (nonatomic, weak) XCTabBarView *tabBarView;

@property (nonatomic, strong) UILongPressGestureRecognizer *longPress;
@property (nonatomic, strong) UITapGestureRecognizer *tapPress;
@end

@implementation CYTabBarVController

- (UILongPressGestureRecognizer *)longPress {
    if (!_longPress) {
        _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick)];
        _longPress.minimumPressDuration = .5;
    }
    return _longPress;
}

- (UITapGestureRecognizer *)tapPress {
    if (!_tapPress) {
        _tapPress = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPressClick)];
        _tapPress.numberOfTapsRequired = 2;
    }
    return _tapPress;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加底部按钮
    [self addTabBar];
    
    self.selectedIndex = 1;//设置启动时默认选中的Item
    
    //监听通话，删除按钮隐藏通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabBarCanChange:) name:CYTabBarChangeNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTabbar) name:@"item" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetTabbar) name:@"tabBar" object:nil];
}

- (void)tabBarCanChange:(NSNotification *)nofi {
    NSString *show = nofi.userInfo[CYEditTabBarShow];
    NSArray *arr = self.tabBarView.subviews;
    XCTabBarButton *btn1 = arr[2];
    XCTabBarButton *btn2 = arr.lastObject;
    XCTabBarButton *btn3 = arr[1];
    
    // 原tabbar 发现按钮
    XCTabBarButton *btn4 = arr[3];
    
    if (show.intValue) {
        [btn1 setImage:[UIImage imageNamed:@"ic_dail_invisibility_normal"] forState:UIControlStateNormal];
        [btn1 setImage:[UIImage imageNamed:@"ic_dail_invisibility_normal"] forState:UIControlStateSelected];
        [btn1 setTitle:CYTabBarTitleD forState:UIControlStateNormal];
        [btn1 setTitleColor:CYNaviTitleColor forState:UIControlStateSelected];
        [btn1 setBackgroundColor:CYNaviThemeColor];
        btn1.selected = YES;
        //改变通话按钮占两个按钮的位置
        btn1.width = btn3.width * 3;
        btn1.x = btn3.x;
        
        [btn2 setImage:[UIImage imageNamed:@"ic_delete_character_sel"] forState:UIControlStateNormal];
        [btn2 setImage:[UIImage imageNamed:@"ic_delete_character_sel"] forState:UIControlStateSelected];
        [btn2 setTitle:CYTabBarTitleE forState:UIControlStateNormal];
        btn2.selected = YES;
        
        //  当输入电话号码时, 将原tabbar中发现按钮隐藏
        [btn4 setHidden:YES];
        
        //添加长按事件
        [btn2 addGestureRecognizer:self.longPress];
    }else {
        //  当无输入电话号码时, 显示 原tabbar中"信用查询"按钮
        [btn1 setImage:[UIImage imageNamed:@"ic_tab_creditsystem_nomal"] forState:UIControlStateNormal];
        [btn1 setImage:[UIImage imageNamed:@"ic_tab_creditsystem_pressed"] forState:UIControlStateSelected];
        
        //  当无输入电话号码时, 显示 "信用查询"标题
        [btn1 setTitle:CYTabBarTitleG forState:UIControlStateNormal];
        [btn1 setTitleColor:CYNaviThemeColor forState:UIControlStateSelected];
        [btn1 setBackgroundColor:[UIColor clearColor]];
        btn1.selected = NO;
        //还原通话按钮的位置,通过通知调用XCTabBarView的layoutSubviews方法还原
        
        [btn2 setImage:[UIImage imageNamed:@"ic_tab_money_normal"] forState:UIControlStateNormal];
        [btn2 setImage:[UIImage imageNamed:@"ic_tab_me_sel"] forState:UIControlStateSelected];
        [btn2 setTitle:CYTabBarTitleC forState:UIControlStateNormal];
        btn2.selected = NO;
        
        //  当无电话号码输入时, 显示原tabbar中发现按钮
        [btn4 setHidden:NO];
        
        //清除长按事件
        [btn2 removeGestureRecognizer:self.longPress];
        
        //设置按钮延迟点击
        [self retardTouchEvent:btn2];
    }
}

//- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
//    if ([self.tabBarDelegate respondsToSelector:@selector(tabBarController:didSelectTo:)]) {
//        [self.tabBarDelegate tabBarController:self didSelectTo:];
//    }
//}

- (void)addTabBar {
    XCTabBarView *tabBarView = [[XCTabBarView alloc] init];
    tabBarView.frame = self.tabBar.bounds;
    tabBarView.delegate = self;
    [self.tabBar addSubview:tabBarView];
    [tabBarView addTabBarButtonWithImageName:@"icon_bh_1" andSelImageName:@"icon_bh_2" andTitle:CYTabBarTitleA];
    [tabBarView addTabBarButtonWithImageName:@"ic_tab_contacts_normal" andSelImageName:@"ic_tab_contacts_pressed" andTitle:CYTabBarTitleB];
    [tabBarView addTabBarButtonWithImageName:@"ic_tab_creditsystem_nomal" andSelImageName:@"ic_tab_creditsystem_pressed" andTitle:CYTabBarTitleG];
    [tabBarView addTabBarButtonWithImageName:@"ic_tab_idea_normal" andSelImageName:@"ic_tab_idea_pressed" andTitle:CYTabBarTitleF];
    [tabBarView addTabBarButtonWithImageName:@"ic_tab_user_nomal" andSelImageName:@"ic_tab_user_pressed" andTitle:CYTabBarTitleC];
//     NSNotificationCenter  * notificationCenter1 = [ NSNotificationCenter  defaultCenter];
//     [notificationCenter1 addObserver: self  selector: @selector(update) name:@"item"  object: nil ];
     
}

- (void)updateTabbar {
     UIButton *btn = self.tabBarView.subviews[4];
     [btn setImage:[UIImage imageNamed:@"ic_tab_user_nomal_hong"] forState:UIControlStateNormal];
     [btn setImage:[UIImage imageNamed:@"ic_tab_user_pressed_hong"] forState:UIControlStateSelected];
}
- (void)resetTabbar {
     UIButton *btn = self.tabBarView.subviews[4];
     [btn setImage:[UIImage imageNamed:@"ic_tab_user_nomal"] forState:UIControlStateNormal];
     [btn setImage:[UIImage imageNamed:@"ic_tab_user_pressed"] forState:UIControlStateSelected];
}

#pragma mark - XCTabBarDelegate
- (void)tabBar:(XCTabBarView *)tabBar didSelectButtonFrom:(NSInteger)from to:(NSInteger)to {
    self.tabBarView = tabBar;
    XCTabBarButton *btn1 = tabBar.subviews[to];
    if (![btn1.titleLabel.text isEqualToString:CYTabBarTitleD] && ![btn1.titleLabel.text isEqualToString:CYTabBarTitleE]) {
        self.selectedIndex = to;
        if ([self.tabBarDelegate respondsToSelector:@selector(tabBar:didSelectTo:)]) {
            [self.tabBarDelegate tabBar:tabBar didSelectTo:to];
        }
    }
    if (to == CYTabBarItemTagA) {
        //        XCTabBarButton *view = self.tabBar.subviews[to];
        //        NSArray *subs = self.tabBar.subviews;
        //        XCTabBarButton *btn = view.subviews[0];
        //        [btn setImage:[UIImage imageNamed:@"ic_dial_keybord_up"] forState:UIControlStateSelected];
        //        NSArray *subs = tabBar.subviews;
        //        CYLog(@"%@", subs);
    }
}

#pragma mark - XCTabBarDelegate
- (void)tabBar:(XCTabBarView *)tabBar buttonClicked:(UIButton *)sender {
    if ([self.tabBarDelegate respondsToSelector:@selector(tabBarController:tabBarClicked:)]) {
        [self.tabBarDelegate tabBarController:self tabBarClicked:sender];
    }
}

//长按事件
- (void)longPressClick {
    if ([self.tabBarDelegate respondsToSelector:@selector(tabBarController:clickStatus:)]) {
        [self.tabBarDelegate tabBarController:self clickStatus:0];
    }
}

/**
 *  设置按钮延迟点击
 */
- (void)retardTouchEvent:(UIButton *)btn {
    //设置btn 400毫秒内不可点击
    btn.userInteractionEnabled = NO;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(400 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
            btn.userInteractionEnabled = YES;
        });
    });
}

- (void)tapPressClick {
    CYLog(@"双击");
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
